package com.xkdx.guangguang.fragment.my.setup;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class SetUpIpFragment extends BaseFragment implements OnClickListener {

	private View view;
	private Button back, im_qr;
	private EditText ed_ip;

	private SharePrefenceUtil sp;
	private String pos_ip;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = new SharePrefenceUtil(getActivity(), IConstants.SP_POS);
		pos_ip = sp.getPosIP();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.pos_ip, container, false);
		findViews(view);
		return view;
	}

	private void findViews(View view) {
		ed_ip = (EditText) view.findViewById(R.id.ed_pos_ip_init);

		if (TextUtils.isEmpty(pos_ip)) {
			ed_ip.setHint(getResources().getString(R.string.input_ip));
		} else {
			ed_ip.setText(pos_ip);
		}
		ed_ip.setText(pos_ip);
		im_qr = (Button) view.findViewById(R.id.im_pos_qr);
		back = (Button) view.findViewById(R.id.ed_posip_back);
		im_qr.setOnClickListener(this);
		back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ed_posip_back: // 返回按钮

			FragmentManager fm = getFragmentManager();
			fm.popBackStack();
			break;

		case R.id.im_pos_qr: // 提交验证码和新密码(重置密码 )
			if (!isIPAvilidate()) {
				return;
			} else {

				IConstants.POS_ADDRESS = ed_ip.getText().toString();
				sp.setPosIP(IConstants.POS_ADDRESS);
				Toast.makeText(getActivity(), "POS机IP地址初始化成功", 0).show();
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 判断提交按钮相关是否合法
	 * 
	 * @return true合法 , false不合法
	 */
	private boolean isIPAvilidate() {

		String ip = ed_ip.getText().toString().trim();
		if (TextUtils.isEmpty(ip)) {
			ed_ip.setFocusable(true);
			ed_ip.setFocusableInTouchMode(true);
			ed_ip.requestFocus();
			Toast.makeText(getActivity(), "POS机IP地址不能为空...", Toast.LENGTH_SHORT)
					.show();
			return false;
		} else if (!(ipCheck(ip))) {
			ed_ip.setFocusable(true);
			ed_ip.setFocusableInTouchMode(true);
			ed_ip.requestFocus();
			Toast.makeText(getActivity(), "输入的IP地址格式不正确,请重新输入", 0).show();
			return false;

		}
		return true;
	}

	/**
	 * 验证ip是否合法
	 * 
	 * @param text
	 *            ip地址
	 * @return 验证信息
	 */
	public boolean ipCheck(String text) {

		// 定义正则表达式
		String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
		// 判断ip地址是否与正则表达式匹配
		if (text.matches(regex)) {
			// 返回判断信息
			return true;
		} else {
			// 返回判断信息
			return false;
		}

	}

	@Override
	public void findView() {
	}

	@Override
	public void setOnClick() {
	}

	@Override
	public void showLoading() {
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
	}

}
